/*
 * INSANE - Interactive Structural Analysis Environment
 *
 * Copyright (C) 2003-2004
 * Universidade Federal de Minas Gerais
 * Escola de Engenharia
 * Departamento de Engenharia de Estruturas
 *
 * Author's email :     insane@dees.ufmg.br
 * Author's Website :   http://www.dees.ufmg.br/insane
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
 * 1307, USA.
 *
 */

package br.ufmg.dees.insane.ui.rich.postp.postp_view.dialog;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

import br.ufmg.dees.insane.commons.view.ViewState;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.ColorsPalette;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.InterfacePostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.PostpViewState;

/**
 * A class representing creation dialog on screen.<br>
 * @author Penna, Samuel & Pitangueira, Roque
 * @since september 2004
 */
public final class ColorsPalete8ChooserDialog extends JFrame {
	
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	private InterfacePostp parent;
	private JPanel contentPane; 
	private JButton okButton;
	private JButton cancelButton;
	private JCheckBox full8ColorsPaletteCheckBox = new JCheckBox("Full Palette");
	private JCheckBox bwr8ColorsPaletteCheckBox = new JCheckBox("BWR Palette");
	private JCheckBox grayScale8ColorsPaletteCheckBox = new JCheckBox("Gray Palette");
	private ViewState state;
	
//	*****************************************************************************
	
	/**
	 * Constructor method, calling super with the especific title.
	 *
	 * @param parent The parent of the class.
	 * @see gui.Interface
	 */
	public ColorsPalete8ChooserDialog(InterfacePostp parent, ViewState vs) {
		this.parent = parent;
		this.state = vs;
		Image ico = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Insane64.gif"));
		this.setIconImage(ico);
		this.setSize(new Dimension(500,500));
		this.setResizable(false);
		this.setAlwaysOnTop(true);
		this.setLocationRelativeTo(this);
		this.setTitle("Colors Palette Chooser");
		contentPane = (JPanel)this.getContentPane(); 
		
		ActionListener list1 = new ChoiceListenerFullPaletteCheckBoxOn();  
		full8ColorsPaletteCheckBox.addActionListener(list1);
		ActionListener list2 = new ChoiceListenerBWRPaletteCheckBoxOn(); 
		bwr8ColorsPaletteCheckBox.addActionListener(list2);
		ActionListener list3 = new ChoiceListenerGrayScalePaletteCheckBoxOn(); 
		grayScale8ColorsPaletteCheckBox.addActionListener(list3);
		
		okButton = new JButton("    OK    ");
		cancelButton = new JButton("Cancel");
		
		ColorsPalete8Panel colors = new ColorsPalete8Panel();
		Line8ColorsPanel line = new Line8ColorsPanel();
		
		okButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						approvedPerform();
						Close(event);
					}
				});
		
		cancelButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						Close(event);
					}
				});
		
		contentPane.setLayout(null); 
		addComponent(contentPane,colors, 20,5,400,200);
		addComponent(contentPane,this.full8ColorsPaletteCheckBox, 20,210,95,30); 
		addComponent(contentPane,this.bwr8ColorsPaletteCheckBox, 160,210,95,30);
		addComponent(contentPane,this.grayScale8ColorsPaletteCheckBox, 300,210,95,30);
		addComponent(contentPane,line, 0,250,500,10);
		addComponent(contentPane,okButton, 115,265,80,30);
		addComponent(contentPane,cancelButton, 205,265,80,30);
		
		
		if(((PostpViewState)this.state).getScaleColorsViewState().getPalette().equals("BWR8Colors")){
			this.bwr8ColorsPaletteCheckBox.setSelected(true);
		}else if(((PostpViewState)this.state).getScaleColorsViewState().getPalette().equals("Full8Colors")){
			this.full8ColorsPaletteCheckBox.setSelected(true);
		}else if(((PostpViewState)this.state).getScaleColorsViewState().getPalette().equals("Gray8Colors")){
			this.grayScale8ColorsPaletteCheckBox.setSelected(true);
		}
		
		this.setVisible(true);
		this.setSize(new Dimension(400,340));
		this.setResizable(true);
		
		
	}
	
	/** Add Component Without a Layout Manager (Absolute Positioning) */ 
	private void addComponent(Container container,Component c,int x,int y,int width,int height) 
	{ 
		c.setBounds(x,y,width,height); 
		container.add(c); 
	} 
	
	public void Close(ActionEvent event){
		this.dispose();
	}
	
	/**
	 * This method is executed on OK button click.
	 *
	 */
	public void approvedPerform() {
		try{
			if(this.bwr8ColorsPaletteCheckBox.isSelected()){                
				((PostpViewState)this.state).getScaleColorsViewState().setPalette("BWR8Colors");
				this.parent.getView().getDrawingArea().repaint();
			}else if(this.full8ColorsPaletteCheckBox.isSelected()){                
				((PostpViewState)this.state).getScaleColorsViewState().setPalette("Full8Colors");
				this.parent.getView().getDrawingArea().repaint();
			}else if(this.grayScale8ColorsPaletteCheckBox.isSelected()){                
				((PostpViewState)this.state).getScaleColorsViewState().setPalette("Gray8Colors");
				this.parent.getView().getDrawingArea().repaint();
			}
			
		} catch (Exception err) {
			Toolkit.getDefaultToolkit().beep();
			this.setVisible(false);
		}
	}
	
	private class ChoiceListenerFullPaletteCheckBoxOn implements ActionListener{
		public void actionPerformed(ActionEvent event){
			if(full8ColorsPaletteCheckBox.isSelected()){
				full8ColorsPaletteCheckBox.setSelected(true);
				bwr8ColorsPaletteCheckBox.setSelected(false);
				grayScale8ColorsPaletteCheckBox.setSelected(false);
			}
		}    
	}
	
	private class ChoiceListenerBWRPaletteCheckBoxOn implements ActionListener{
		public void actionPerformed(ActionEvent event){
			if(bwr8ColorsPaletteCheckBox.isSelected()){
				full8ColorsPaletteCheckBox.setSelected(false);
				bwr8ColorsPaletteCheckBox.setSelected(true);
				grayScale8ColorsPaletteCheckBox.setSelected(false);
			}
		}    
	}
	
	private class ChoiceListenerGrayScalePaletteCheckBoxOn implements ActionListener{
		public void actionPerformed(ActionEvent event){
			if(grayScale8ColorsPaletteCheckBox.isSelected()){
				full8ColorsPaletteCheckBox.setSelected(false);
				bwr8ColorsPaletteCheckBox.setSelected(false);
				grayScale8ColorsPaletteCheckBox.setSelected(true);
			}
		}    
	}
	
//	*****************************************************************************
}

class ColorsPalete8Panel extends JPanel{
	
	private static final long serialVersionUID = 1L;
	
	public void paintComponent(Graphics g){
		//super.paintComponents(g);
		//this.setBackground(Color.WHITE);
		
		LinkedList palete = ColorsPalette.colorsPaletteFull8();
		int deltaY = 0;
		for (int i = 0; i < palete.size(); i++) {
			g.setColor((Color)palete.get(i));
			g.fillRect(0,deltaY,40,24);
			deltaY+=24;
		}
		g.setColor(Color.BLACK);
		g.drawRect(0,0,40,192);
		
		palete = ColorsPalette.colorsPaletteBWR8();
		deltaY = 0;
		for (int i = 0; i < palete.size(); i++) {
			g.setColor((Color)palete.get(i));
			g.fillRect(140,deltaY,40,24);
			deltaY+=24;
		}
		g.setColor(Color.BLACK);
		g.drawRect(140,0,40,192);
		
		palete = ColorsPalette.colorsPaletteGrayScale8();
		deltaY = 0;
		for (int i = 0; i < palete.size(); i++) {
			g.setColor((Color)palete.get(i));
			g.fillRect(280,deltaY,40,24);
			deltaY+=24;
		}
		g.setColor(Color.BLACK);
		g.drawRect(280,0,40,192);
		
	}
	
//	*****************************************************************************************
}//end of this class

class Line8ColorsPanel extends JPanel{
	
	private static final long serialVersionUID = 1L;
	
	public void paintComponent(Graphics g){
		g.setColor(Color.GRAY);
		g.drawLine(0,0,1000,0);
		
	}
	
//	*****************************************************************************************
}//end of this class
